#include <Foundation/Foundation.h>
#include <Foundation/NSObject.h>
#include <stdio.h>

#import "CheckersGameState.h" 

@implementation CheckersGameState

@synthesize player1;
@synthesize player2;
@synthesize selectPosition;
@synthesize pieceIsSelected;
@synthesize isPaused;
@synthesize moveAmount;

-(id) init {
    if ( self = [super init]){
        chbInitBoard(&chb);
        moveAmount = 0;
    }
    return self;
}

-(BOOL) makeMove : (CheckersMove*) move{ 
    if (!chbIsValidMove(&chb, move)) {
        NSLog(@"Invalid move!!");
        return NO;
    }
    chbMakeMove(&chb, move, &unmoves[moveAmount]);
    moveAmount++;
    pieceIsSelected = NO;
    
    return YES;
}
-(BOOL) unmakeMove { 
    if (moveAmount < 1) return NO;
    
    moveAmount--;
    chbUnmakeMove(&chb, &unmoves[moveAmount]);
    [player1 invalidateNextMove];
    [player2 invalidateNextMove];
    return YES;
}
-(CheckersBoard*) getBoardRef{
    return &chb;
}

-(id<CheckersPlayer>) currentPlayer{
    return chb.iswhiteturn ? player1 : player2;
}


-(void) togglePauseState {
    isPaused = !isPaused;
    
    [player1 setPaused: isPaused];
    [player2 setPaused: isPaused];
    
    [player1 invalidateNextMove];
    [player2 invalidateNextMove];
}
@end


